package com.primb.bench.common.module.repository;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.entity.ClientInfoEntity;
import com.primb.bench.project.home.entity.EnsureInfoEntity;
import com.primb.bench.project.home.entity.HttpLoanTreeEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.bench.project.home.entity.LoanInfoEntity;
import com.primb.bench.project.home.entity.LogoInfoEntity;
import com.primb.bench.project.home.entity.TaxInfoEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Chen on 2017/5/24.
 * 功能描述：
 */

public class SearchConditionRepository {

    public static Observable<List<KeyEntity>> getSearchCondition(int code) {
        List<KeyEntity> dataList = new ArrayList<>();
        switch (code) {
            case 0:
                return ApiClient.initService(ApiService.class).getLoanBusinessList("").flatMap(new Func1<HttpLoanTreeEntity, Observable<List<KeyEntity>>>() {
                    @Override
                    public Observable<List<KeyEntity>> call(HttpLoanTreeEntity httpEntity) {
                        try {
                            convertList(dataList, httpEntity.getData());
                        } catch (Exception e) {
                            Observable.error(new ApiException("服务器数据异常，请联系管理员解决"));
                        }
                        return Observable.just(dataList);
                    }
                }).compose(RxSchedulerUtils.normalSchedulersTransformer());
            case 1:
                dataList.add(new KeyEntity("贷款定价试算", "01"));
                dataList.add(new KeyEntity("贷款定价申请", "02"));
                break;
            case 2:
                dataList.add(new KeyEntity("年", "0"));
                dataList.add(new KeyEntity("月", "1"));
                break;
            case 3:
                return ApiClient.initService(ApiService.class).getLoanPartList().flatMap(new Func1<HttpLoanTreeEntity, Observable<List<KeyEntity>>>() {
                    @Override
                    public Observable<List<KeyEntity>> call(HttpLoanTreeEntity httpEntity) {
                        try {
                            convertList(dataList, httpEntity.getData());
                        } catch (Exception e) {
                            Observable.error(new ApiException("服务器数据异常，请联系管理员解决"));
                        }
                        return Observable.just(dataList);
                    }
                }).compose(RxSchedulerUtils.normalSchedulersTransformer());
        }
        return Observable.just(dataList).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static void convertList(List<KeyEntity> dataList, List<HttpLoanTreeEntity.DataBean> entity) {
        for (int i = 0; i < entity.size(); i++) {
            if (entity.get(i).getLeafFlag() != null && entity.get(i).getLeafFlag().equals("1")) {
                dataList.add(new KeyEntity(entity.get(i).getText(), entity.get(i).getId()));
            } else {
                convertList(dataList, entity.get(i).getChildren());
            }
        }
    }
}
